﻿using System;
using System.Windows;
using Innvoice.Logica.GestorDB;
using Innvoice.Logica.GestorObject.Articulo;
using System.Windows.Input;
using System.ComponentModel;

namespace Innvoice.Ws.Articulos.SubArticulos
{
    /// <summary>
    /// Interaction logic for AltaArticulo.xaml
    /// </summary>
    public partial class AltaSubArticulo : Window
    {

        #region Property

        private SubArticulo subArticulo = new SubArticulo();
        private ArticuloDB articuloDB = new ArticuloDB();

        #endregion

        #region Contructor

        public AltaSubArticulo()
        {
            InitializeComponent();
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            subArticulo.PropertyChanged += new PropertyChangedEventHandler(subArticulo_PropertyChanged);
            DataContext = subArticulo;
            textDescripcionSubArticulo.Focus();

        }

        #endregion

        #region Windows Methods

        private void btnGuardar_Click(object sender, RoutedEventArgs e)
        {
            GuardarElemento();
        }

        private void btnCancelar_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            articuloComboBox.Items.Clear();
            articuloComboBox.ItemsSource = articuloDB.ObtenerArticulosSegunEstado(1);
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key.ToString().ToUpper())
            {
                case "F1":
                    GuardarElemento();
                    break;
                case "ESCAPE":
                    this.Close();
                    break;
                default:
                    return;
             }
        }

        private void subArticulo_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            btnGuardar.IsEnabled = subArticulo.IsValid();
        }

        #endregion

        #region Auxiliar Methods

        private void GuardarElemento()
        {
            subArticulo.CodigoArticulo = Convert.ToInt32(articuloComboBox.SelectedValue);
            
            articuloDB.AddSubArticulo(subArticulo);
            this.Close();
        }

        #endregion  
    }
}
